package com.lg.sysfile.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.sysfile.entity.SysFile;
import com.lg.sysfile.service.SysFileService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

import me.chanjar.weixin.common.exception.WxErrorException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-04-08
 */
@RestController
@RequestMapping("/sysFile")
public class SysFileController extends BaseController<SysFile, SysFileService> {

    private final String prefix="sysfile/";
    
	@Autowired
	SysFileService sysFileService;

	@RequiresPermissions("sysFile:sysFile")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("sysFile:sysFile")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody SysFile  sysFile){
		Query<SysFile> query = new Query<SysFile>(sysFile);
		List<SysFile> sysFiles=sysFileService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(sysFiles,query.getPage().getTotal());
		return pa;
	}
	
	@GetMapping("/add/{type}")
	public ModelAndView add(@PathVariable("type") String type){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	
	@RequiresPermissions("sysFile:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", sysFileService.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("sysFile:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) SysFile sysFile,BindingResult bindingResult){
		sysFile.setCreateId(getUserId());
		if(sysFileService.save(sysFile)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("sysFile:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) SysFile sysFile,BindingResult bindingResult){
		sysFile.setModifiedId(getUserId());
		if(sysFileService.update(sysFile)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("sysFile:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) SysFile sysFile,BindingResult bindingResult){
		
		if(sysFileService.deleteById(sysFile.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("sysFile:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(sysFileService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@ResponseBody
	@PostMapping("/upload")
	R upload(@RequestParam("file") MultipartFile files, HttpServletRequest request) throws IOException {
		SysFile sysFile= sysFileService.upload(files);
		if(sysFile.getId()!=null){
			return R.ok(sysFile.getLocalUrl());//上传成功之后本地路径
		}else{
			return R.error();
		}
		
	}


}
